#ifndef __REDISCONF_H__
#define __REDISCONF_H__
#include <string>
#include <yaml-cpp/yaml.h>

#include "config.h"

class RedisConf
{
public:
    typedef std::shared_ptr<RedisConf> ptr;
    const std::string& GetHost() const
    {
        return m_host;
    }

    void SetHost(std::string host)
    {
        m_host = host;
    }

    const int GetPort() const
    {
        return m_port;
    }

    void SetPort(int port)
    {
        m_port = port;
    }

    const std::string& GetPasswd() const
    {
        return m_passwd;
    }

    void SetPasswd(std::string passwd)
    {
        m_passwd = passwd;
    }

    const long GetConnectTimeOut() const
    {
        int timeout = m_connectTimeOut.tv_sec * 1000 + m_connectTimeOut.tv_usec / 1000;
        return timeout;
    } 

    const timeval GetConnectTimevalOut() const
    {
        return m_connectTimeOut;
    }

    void SetConnectTimeOut(long timeout)
    {
        m_connectTimeOut = {timeout / 1000, timeout % 1000 * 1000};
    }
private:
    std::string m_host;
    int m_port;
    std::string m_passwd;
    struct timeval m_connectTimeOut;
};

template<>
class LexicalCast<std::string, RedisConf>
{
public:
    RedisConf operator()(const std::string& str)
    {
        YAML::Node node = YAML::Load(str);
        RedisConf redisConf;
        redisConf.SetHost(node["host"].as<std::string>());
        redisConf.SetPort(node["port"].as<int>());
        redisConf.SetPasswd(node["passwd"].as<std::string>());
        redisConf.SetConnectTimeOut(node["connecttimeout"].as<long>());
        return redisConf;
    }
};

template<>
class LexicalCast<RedisConf, std::string>
{
public:
    std::string operator()(const RedisConf& redisConf)
    {
        YAML::Node node;
        std::stringstream ss;
        node["host"] = redisConf.GetHost();
        node["port"] = redisConf.GetPort();
        node["passwd"] = redisConf.GetPasswd();
        node["connecttimeout"] = redisConf.GetConnectTimeOut();
        ss << node;
        return ss.str();
    }
};

#endif // !__REDISCONF_H__
